from typing import *


class Solution:

    def findMaximumXOR(self, nums: List[int]) -> int:

        class Node(dict):
            pass

        root = Node()

        def add(num):
            node = root
            for i in range(30, -1, -1):
                c = (num >> i) & 1
                if c not in node:
                    node[c] = Node()
                node = node[c]

        def find(num):
            node = root
            ans = 0
            for i in range(30, -1, -1):
                c = (num >> i) & 1
                if c ^ 1 in node:
                    ans |= 1 << i
                    node = node[c ^ 1]
                else:
                    node = node[c]
            return ans

        for num in nums:
            add(num)

        ans = 0

        for num in nums:
            ans = max(ans, find(num))

        return ans
